<?php
	require_once("../conexion/conexion.php");
    require_once("../Mantenedores/cliente.php");
	require_once("../Funciones/funciones_publicas.php");
    
    $cli = new Cliente();
	$fun = new funciones_publicas();

	if(isset($_GET['num']))
	{ $facNum = $_GET['num'];}
	if(isset($_GET['fecha']))
	{ $fechaFactura = $_GET['fecha'];}
   	
	$fecha = explode("/",$fechaFactura);
    $fecha = "$fecha[2]-$fecha[1]-$fecha[0]";
	
	//se busca los datos del cliente
	$con = new Conexion();
	$con2 = $con->conexionBD("fpproduccion");
	$sql="select 
				CliRSocial,
				CliDireccion,
				CliGiro,
				ConVentaDescrip,
				f.CliRut,
				CliComuna,
				CliFono,
				AENombre,
				date_format(FacFecha,'%d-%m-%y') as fecha
				
				from clientes c
				inner join factura f on 
				f.CliRut=c.CliRut 
				inner join condicionventa cv on 
				cv.ConVentaCod=f.ConVentaCod
				inner join areaextraccion ae on
				ae.AECod=f.AECod
				
				where FacCod='".$facNum."'";
	$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
	$datos =   mysql_fetch_array($res);
	$nombre = utf8_decode($datos[0]);
	$dir = utf8_decode($datos[1]);
	$giro = utf8_decode($datos[2]);
	$comuna = utf8_decode($datos[5]);
	$area = utf8_decode($datos[7]);
	$con->cerrarBD();
	
	$fechaFac = explode("-",$datos[8]);
	$dia = $fechaFac[0];
	$mes = $fechaFac[1];
	$agno = $fechaFac[2];
	$palMes = $fun->SeleccionaMes($mes);
	$palMes2 = $fun->SeleccionaMes($mes-1);
	$agno2 = date('Y');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<!--<link href="../Estilos/global.css" rel="stylesheet" type="text/css" />-->
<!--<link href="../Estilos/local.css" rel="stylesheet" type="text/css" />-->
<script src="../include_js/jquery.js" type="text/javascript"></script>
<style>
label.texto
{
    font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
.caja
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
	text-decoration: none;
	background-color: #FFFFFF;
	border: 0px;
	border-left: 0px solid #6C6C6C;
	border-right: 0px solid #6C6C6C;
	text-align:right;
}
.area
{
	font-family:Arial;
	font-style:normal;
	font-size:9pt;
	color:#000000;
	border-style:solid; 
	border-top-width:0px; 
	border-right-width:0px; 
	border-bottom-width:0px;
	border-left-width:0px;
	overflow:hidden;
}
.texto
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
	text-align:center;
}
.datos
{
	font-family:Arial;
	font-style:normal;
	font-size:8pt;
	color:#000000;
	text-align:center;
}
.resp
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
.digito
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
</style>
<script>
function window.onbeforeprint()
{ 
	boton=document.getElementById("btn_Imprimir");
	boton.style.display = 'none'; 
} 
function window.onafterprint()
{ 
	boton=document.getElementById("btn_Imprimir");
	boton.style.display = 'block'; 
}

function Imprimir()
{
	window.print()
}
</script>
</head>

<body>
<form name="form_VentanFactura" id="form_VentanFactura" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
  <td align="center"><input name="btn_Imprimir" id="btn_Imprimir" type="button" onclick="Imprimir();" value="Imprimir" /></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2" style="height:190px;">&nbsp;</td>
  </tr>
  <tr>
    <td width="79%">&nbsp;</td>
    <td width="21%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><div style="padding-left:30px"><label class="texto"><?php echo $dia;?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="texto"><?php echo $palMes;?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="texto"><?php echo $agno;?></label></div></td>
  </tr>
  <tr>
    <td><div style="padding-left:50px; padding-top:12px;"><label class="datos"><?php echo $nombre;?></label></div></td>
    <td><div style="padding-top:12px;"><label class="digito"><?php echo $datos[4];?></label></div></td>
  </tr>
  <tr>
    <td><div style="padding-left:50px;padding-top:12px;"><label class="datos"><?php echo $dir;?></label></div></td>
    <td><div style="padding-top:12px;"><label class="datos"><?php echo $comuna;?></label></div></td>
  </tr>
  <tr>
    <td><div style="padding-left:25px;padding-top:12px;"><label class="datos"><?php echo $giro;?></label></div></td>
    <td><div style="padding-top:12px;"><label class="digito"><?php echo $datos[6];?></label></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div style="padding-top:12px;"><label class="datos"><?php echo $datos[3];?></label></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

<!--tabla que contiene la glosa de la factura -->
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td colspan="4" style="height:40px;">&nbsp;</td>
</tr>
<tr>
	<td width="12%">&nbsp;</td>
	<td width="56%">&nbsp;</td>
	<td width="15%">&nbsp;</td>
	<td width="17%">&nbsp;</td>
</tr>
<?php
	$sumatoria=0;
	//PSMB
	$con = new Conexion();
	$con2 = $con->conexionBD("fpproduccion");
	$sql = "select FacPsmb,FacPorcentaje from factura where FacFecha='".$fecha."' and CliRut='".$datos[4]."' and FacCod='".$facNum."'";
	$res2= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
	while($row = mysql_fetch_array($res2))
	{
		$cadena = "Muestreos y analisis muestras Programa de Moluscos Bivalvos PSMB, Area ".$area." mes ".$palMes2." del ".$agno2.", correspondiente a un % ".$row['FacPorcentaje']."";
		//$cadena .= "PSMB, Area ".$datos[7]." mes ".$palMes." del ".$agno.", correspondiente ";
		//$cadena .= "a un % ".$row['FacPorcentaje']."";
		echo "<tr>";
		echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>1</label></td>";
		echo "<td width='56%' class='resp'><div style='width:350px;'>".$cadena."</div></td>";
		echo "<td width='15%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($row['FacPsmb'],0,',','.')."</label></td>";
		echo "<td width='17%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($row['FacPsmb'],0,',','.')."</label></td>";
		echo "</tr>";
		$sumatoria = $sumatoria + $row['FacPsmb'];
	}
	$con->cerrarBD();
	
	echo "<tr><td colspan='4'>&nbsp;</tr>";

	//valores del muestreo
	$con = new Conexion();
	$con2 = $con->conexionBD("fpproduccion");
	$sql = "select FacCantMuestreo,FacPorcentaje,FacUFMuestreo,FacUF,FacTotalMuestreo from factura where FacFecha='".$fecha."' and CliRut='".$datos[4]."' and FacCod='".$facNum."'";
	$res3= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
	while($row = mysql_fetch_array($res3))
	{
		$unitario = ($row['FacUFMuestreo']*$row['FacUF']);
		$unitario = round($unitario);
		$total = ($row['FacPorcentaje']*$row['FacTotalMuestreo'])/100;
		$total = round($total);
		echo "<tr>";
		echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>".$row['FacCantMuestreo']."</label></td>";
		echo "<td width='56%' class='resp'>Valor del Muestreo Fishing, correspondiente a un %".$row['FacPorcentaje']."</td>";
		echo "<td width='15%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
		echo "<td width='17%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
		echo "</tr>";
		$sumatoria = $sumatoria + $total;
	}
	$con->cerrarBD();
			
	echo "<tr><td colspan='4'>&nbsp;</tr>";
	
	//se cargan los otros valores
	$con = new Conexion();
	$con2 = $con->conexionBD("fpproduccion");
	$sql = "select ItemFacCant,ItemFacGlosa,ItemFacUnitario,ItemFacTotal,FacPorcentaje from itemfactura inner join factura on factura.FacCod=itemfactura.FacCod where factura.FacFecha='".$fecha."' and factura.CliRut='".$datos[4]."' and itemfactura.FacCod='".$facNum."'";
	$res4 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
	while($row = mysql_fetch_array($res4))
	{
		$total = ($row['FacPorcentaje']*$row['ItemFacTotal'])/100;
		$total = round($total);
		echo "<tr>";
		echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>".$row[0]."</label></td>";
		echo "<td width='56%' class='resp'>".$row[1]."</td>";
		echo "<td width='15%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
		echo "<td width='17%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
		echo "</tr>";
		$sumatoria = $sumatoria + $total;
	}
	$con->cerrarBD();
			
	$subTotal = $sumatoria;
	$iva = round($subTotal*0.19);
	$total = ($subTotal+$iva);
	$pal=$fun->numletras($total);
?>
</table>
<div style="position:absolute; top:710px; left:80px; width:350px"><label style="font-family:Arial; font-size:9pt; font-style:italic;">DEPOSITAR EN CUENTA CTE N&deg; 60310391 BANCO BCI A FISHING PARTNERS LTDA</label></div>
<div style="position:absolute; top:805px; left:10px"><textarea name="txtPalabras" cols="70" rows="3" class="area"><?php echo $pal;?></textarea></div>
<div style="position:absolute; top:810px; left:520px;"><input name="txtSubTotal" type="text" class="caja" readonly="true"  size="12" value="<?php echo number_format ($subTotal,0,',','.')?>"/></div>
<div style="position:absolute; top:840px; left:520px;"><input name="txtIVA" type="text" class="caja" readonly="true" size="12" value="<?php echo number_format ($iva,0,',','.')?>"/></div>
<div style="position:absolute; top:870px; left:520px;"><input name="txtTotal" type="text"  class="caja" readonly="true" size="12" value="<?php echo number_format ($total,0,',','.')?>" /></div>
<div style="position:absolute; top:844px; left:441px;"><input name="txtImp" type="text"  class="caja" readonly="true" size="1" value="19" /></div>
</form>
</body>
</html>